#!/usr/bin/env bash

help(){
    cat<<__EOF__
Usage: m dock [OPTIONS] [ARGUMENTS]

Description: Manage the macOS Dock settings and items

Options:
  --autohide <enable|disable>       Enable or disable the Dock's auto-hide feature
  --autohidedelay SECONDS           Changes how long it takes to detect you want to show up the dock
  --additem PATH                    Add an item to the Dock, e.g. /Applications/Safari.app
  --addblankspace                   Add a blank space (separator) to the Dock
  --addrecentitems                  Add a stack containing your recent items to the Dock
                                     (You can change the stack's type by right-clicking on it)
  --magnification <enable|disable>  Enable or disable magnification of Dock icons
  --position <bottom|left|right>    Change the Dock's position on the screen
  --prune                           Remove all items from the Dock
  --showdelay SECONDS               Changes how long the Dock takes to show up when auto-hide is enabled
__EOF__
}

show_delay(){
    case $1 in
        [0-9][.][0-9])
            echo "New Auto-Hide time: "$1
            defaults write com.apple.dock autohide-time-modifier -float $1
            ;;
        [0-9])
            echo "New Auto-Hide time: "$1
            defaults write com.apple.dock autohide-time-modifier -int $1
            ;;
        *)
            echo "Current Auto-Hide time: $(defaults read com.apple.dock autohide-time-modifier 2>/dev/null)"
            exit 0
            ;;
    esac
    killall Dock
}

auto_hide(){
    case $1 in
        enable)
            echo "Auto Hide: enable"
            defaults write com.apple.dock autohide -boolean YES
            ;;
        disable)
            echo "Auto Hide: disable"
            defaults write com.apple.dock autohide -boolean NO
            ;;
        *)
            echo "Auto Hide: $(defaults read com.apple.dock autohide)"
            exit 0
            ;;
    esac
    killall Dock
}

autohide_delay(){
    case $1 in
    [0-9][.][0-9])
        echo "New Auto-Hide delay time: "$1
        defaults write com.apple.dock autohide-delay -float $1
      ;;
    [0-9])
      echo "New Auto-Hide delay time: "$1
      defaults write com.apple.dock autohide-delay -int $1
      ;;
    *)
      echo "Current Auto-Hide delay time: $(defaults read com.apple.dock autohide-delay 2>/dev/null)"
      exit 0
      ;;
  esac
  killall Dock
}


magnify(){
    case $1 in
        enable)
            echo "Magnification: enable"
            defaults write com.apple.dock magnification -boolean YES
            ;;
        disable)
            echo "Magnification: disable"
            defaults write com.apple.dock magnification -boolean NO
            ;;
        *)
            echo "Magnification: $(defaults read com.apple.dock magnification)"
            exit 0
            ;;
    esac
    killall Dock
}

dock_position(){
    case $1 in
        [bB][oO][tT][tT][oO][mM])
            echo "Dock Position: BOTTOM"
            defaults write com.apple.dock orientation bottom
            ;;
        [lL][eE][fF][tT])
            echo "Dock Position: LEFT"
            defaults write com.apple.dock orientation left
            ;;
        [rR][iI][gG][hH][tT])
            echo "Dock Position: RIGHT"
            defaults write com.apple.dock orientation right
            ;;
        *)
            echo "Position: $(defaults read com.apple.dock orientation)"
            exit 0
            ;;
    esac
    killall Dock
}

add_blank_space(){
    echo "New blank space added to the Dock"
    defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'
    killall Dock
}

add_item(){
    [ -z "$1" ] && help && exit 1
    echo "Item added to the Dock: $1"
    defaults write \
        com.apple.dock \
        persistent-apps \
        -array-add \
        "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>$1</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
    killall Dock
}

add_recent_items(){
    echo "Add recent items stack"
    defaults write com.apple.dock persistent-others -array-add '{"tile-data" = {"list-type" = 1;}; "tile-type" = "recents-tile";}';
    killall Dock
}

prune(){
    echo "remove all items from stack"
    defaults write com.apple.dock persistent-apps '()'
    killall Dock
}

## Main script execution
option="${1}"
shift

case "${option}" in
    --help)
        help
        ;;
    --showdelay)
        show_delay $@
        ;;
    --autohide)
        auto_hide $@
        ;;
    --autohidedelay)
        autohide_delay $@
        ;;
    --magnification)
        magnify $@
        ;;
    --position)
        dock_position $@
        ;;
    --addblankspace)
        add_blank_space
        ;;
    --additem)
        add_item $@
        ;;
    --addrecentitems)
        add_recent_items
        ;;
    --prune)
        prune
        ;;
    *)
        help && exit 1
        ;;
esac
